package com.qkbb.admin.kuibu.adapter;

import android.content.Context;
import android.content.SharedPreferences;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.lidroid.xutils.HttpUtils;
import com.lidroid.xutils.exception.HttpException;
import com.lidroid.xutils.http.ResponseInfo;
import com.lidroid.xutils.http.callback.RequestCallBack;
import com.lidroid.xutils.http.client.HttpRequest;
import com.qkbb.admin.kuibu.R;
import com.qkbb.admin.kuibu.qkbb.JavaBean.InVite;
import com.qkbb.admin.kuibu.qkbb.funcation.Friends;
import com.qkbb.admin.kuibu.qkbb.funcation.ImageDownloadSDCardCacheHelper;
import com.qkbb.admin.kuibu.qkbb.funcation.OSShelp;
import com.qkbb.admin.kuibu.qkbb.funcation.SDCardHelper;
import com.qkbb.admin.kuibu.qkbb.url.Url;

import java.net.MalformedURLException;
import java.net.URL;
import java.util.List;

/**
 * Created by admin on 2016/2/24.
 */
public class MeetFriendAdapter extends BaseAdapter{
   List<InVite> list;
    Context context;
    private String user_token;
    private URL url;

    public MeetFriendAdapter(List<InVite> list,Context context) {
        this.list = list;
        this.context=context;
    }

    @Override
    public int getCount() {
        return list.size();
    }

    @Override
    public Object getItem(int position) {
        return list.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }
    ViewHold viewHold;
    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        if (convertView==null){
            convertView= LayoutInflater.from(context).inflate(R.layout.meet_friend_item,null);
            viewHold=new ViewHold();
            viewHold.imageView= (ImageView) convertView.findViewById(R.id.meet_friend_item_image);
            viewHold.nameview= (TextView) convertView.findViewById(R.id.meet_friend_item_name);
            viewHold.newsview= (TextView) convertView.findViewById(R.id.meet_friend_item_news);
            viewHold.acceptview= (TextView) convertView.findViewById(R.id.meet_friend_item_accept);
            viewHold.yitianjia= (TextView) convertView.findViewById(R.id.meet_friend_item_yijieshou);
            convertView.setTag(viewHold);
        }else{
            viewHold= (ViewHold) convertView.getTag();
        }
//        viewHold.imageView.setImageResource(list.get(position).getImage());
        viewHold.newsview.setText(list.get(position).getReason());
        viewHold.nameview.setText(list.get(position).getNickname());
        if (list.get(position).getAddstatus().equals("20")){
            viewHold.yitianjia.setVisibility(View.GONE);
            viewHold.acceptview.setVisibility(View.VISIBLE);
        }else {
            viewHold.acceptview.setVisibility(View.GONE);
            viewHold.yitianjia.setVisibility(View.VISIBLE);
        }
        viewHold.acceptview.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                user_token= SDCardHelper.loadFileFromSdCard("user_token",context);
                Log.e("user_toker", user_token);
                HttpUtils utils=new HttpUtils();
                String uri= Url.POSTACCCEPTFRIEND+user_token+"&friendid="+list.get(position).getFriendid()+"&invitationid="+list.get(position).getInvitationid();
                Log.e("posturl", uri);
                try {
                   url=new URL(uri);
                } catch (MalformedURLException e) {
                    e.printStackTrace();
                }
                utils.send(HttpRequest.HttpMethod.POST, url.toString(), new RequestCallBack<String>() {
                    @Override
                    public void onSuccess(ResponseInfo<String> responseInfo) {
                        Log.e("fshsaj", responseInfo.result);
                        viewHold.acceptview.setText("已添加");

                        Toast.makeText(context, "添加成功", Toast.LENGTH_SHORT).show();
                    }

                    @Override
                    public void onFailure(HttpException e, String s) {

                    }
                });
            }
        });
        ImageDownloadSDCardCacheHelper imageDownloadSDCardCacheHelper=new ImageDownloadSDCardCacheHelper();
        InVite inVite=list.get(position);
        String url=new OSShelp(context).getBitmap(inVite.getPhoto());
        imageDownloadSDCardCacheHelper.myDownloadImageSDCardCache(context,url,viewHold.imageView,inVite.getPhoto(),1);
        return convertView;
    }
    class ViewHold{
        ImageView imageView;
        TextView nameview;
        TextView newsview;
        TextView acceptview;
        TextView yitianjia;
    }
}
